using Microsoft.EntityFrameworkCore;
using Wordss.Models;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddControllers();

builder.Services.AddDbContext<Wode>(options=>{
    var cooners = 
    builder.Configuration.GetConnectionString("DefaultConnection");
    options.UseMySql(cooners,ServerVersion.AutoDetect(cooners));
   
});


var app = builder.Build();

using (var scope=app.Services.CreateScope()){
    var services =scope.ServiceProvider;
    var context =services.GetRequiredService<Wode>();
    context.Database.EnsureCreated();
    seedData(context);
};


// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();





app.MapControllers();
app.Run();

void seedData(Wode wode){
   if (!wode.Blogs.Any())
   {
    wode.Blogs.AddRange(
new Blog{Id=1,Name="23",Age="1"},
new Blog{Id=2,Name="23",Age="1"},
new Blog{Id=4,Name="23",Age="1"}
    );

    wode.SaveChanges();

   }
}
